home *** CD-ROM | disk | FTP | other *** search
/ Personal Computer World 2008 February / PCWFEB08.iso / Software / Freeware / Miro 1.0 / Miro_Installer.exe / xulrunner / python / httpauth.py < prev    next >
Encoding:
Python Source  |  2007-11-12  |  2.1 KB  |  54 lines

  1. # Miro - an RSS based video player application
  2. # Copyright (C) 2005-2007 Participatory Culture Foundation
  3. #
  4. # This program is free software; you can redistribute it and/or modify
  5. # it under the terms of the GNU General Public License as published by
  6. # the Free Software Foundation; either version 2 of the License, or
  7. # (at your option) any later version.
  8. #
  9. # This program is distributed in the hope that it will be useful,
  10. # but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  12. # GNU General Public License for more details.
  13. #
  14. # You should have received a copy of the GNU General Public License
  15. # along with this program; if not, write to the Free Software
  16. # Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301 USA
  17.  
  18. from download_utils import parseURL
  19. import dialogs
  20. import eventloop
  21. import views
  22.  
  23. def formatAuthString(auth):
  24.     return "%s %s" % (auth.getAuthScheme(), auth.getAuthToken())
  25.  
  26. def findHTTPAuth(callback, host, path):
  27.     """Find an HTTPAuthPassword object stored in the database.  Callback will
  28.     be called with a string to use for the Authorization header or None if we
  29.     can't find anything in the DB.
  30.     """
  31.     import downloader
  32.  
  33.     auth = downloader.findHTTPAuth(host, path)
  34.     if auth is not None:
  35.         auth = formatAuthString(auth)
  36.     eventloop.addIdle(callback, "http auth callback", args=(auth,))
  37.  
  38. def askForHTTPAuth(callback, url, realm, authScheme):
  39.     """Ask the user for a username and password to login to a site.  Callback
  40.     will be called with a string to use for the Authorization header or None
  41.     if the user clicks cancel.
  42.     """
  43.  
  44.     scheme, host, port, path = parseURL(url)
  45.     def handleLoginResponse(dialog):
  46.         import downloader
  47.         if dialog.choice == dialogs.BUTTON_OK:
  48.             auth = downloader.HTTPAuthPassword(dialog.username,
  49.                     dialog.password, host, realm, path, authScheme)
  50.             callback(formatAuthString(auth))
  51.         else:
  52.             callback(None)
  53.     dialogs.HTTPAuthDialog(url, realm).run(handleLoginResponse)
  54.